/*
 *  Copyright 2010 asustatil.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package edu.upf.viewer;

import edu.upf.content.ImageObject;
import edu.upf.editor.ImagePanel;
import edu.upf.utils.LogSystem;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

/**
 *
 * @author asustatil
 */
public class ViewerFrame extends javax.swing.JFrame {

    private final static int MAX_VIEWER_HEIGHT=550;
    private final static int MAX_VIEWER_WIDTH=780;
    private final static int MIN_VIEWER_WIDTH=550;
    private final static int EXTRA_WIDTH=5;
    private final static int EXTRA_HEIGHT=75;
    private LogSystem log;
    private ImageObject currentImage;
    private boolean autoDeleteOnClose;
    
    /** 
     * Creates new form ViewerFrame
     * @param title
     * @param autoDeleteOnClose
     */
    public ViewerFrame(String title, boolean autoDeleteOnClose) {
        initComponents();
        log = new LogSystem(this.getClass());
        this.setTitle(title);
        this.autoDeleteOnClose = autoDeleteOnClose;
    }

    public void showImage(ImageObject imageObject){
        currentImage=imageObject;
        String path = imageObject.getPath();
        BufferedImage img=null;
        File file = new File(path);
        try {
            img = ImageIO.read(file);
        } catch (IOException ex) {
            log.addDebugLog(ex.getMessage());
            return;
        }
        ImagePanel ip = new ImagePanel(img);
        imageScrollPane.getViewport().add(ip);
        nameLabel.setText(imageObject.getName());
        sizeLabel.setText(imageObject.getSize());
        albumLabel.setText(imageObject.getAlbum());
        int heigth=img.getHeight(), width=img.getWidth();
        if(img.getHeight()>MAX_VIEWER_HEIGHT){
            heigth=MAX_VIEWER_HEIGHT;
        }
        if(img.getWidth()>MAX_VIEWER_WIDTH){
            width=MAX_VIEWER_WIDTH;
        }
        else if(img.getWidth()<MIN_VIEWER_WIDTH){
            width=MIN_VIEWER_WIDTH;
        }
        this.setSize(width+EXTRA_WIDTH,heigth+EXTRA_HEIGHT);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        imageScrollPane = new javax.swing.JScrollPane();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        nameLabel = new javax.swing.JLabel();
        sizeLabel = new javax.swing.JLabel();
        albumLabel = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        jLabel1.setText("Name:");

        jLabel2.setText("Size:");

        jLabel3.setText("Album:");

        nameLabel.setText("name");

        sizeLabel.setText("size");

        albumLabel.setText("album");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(nameLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(sizeLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(albumLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(55, Short.MAX_VALUE))
            .addComponent(imageScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 594, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(imageScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 351, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(nameLabel)
                    .addComponent(sizeLabel)
                    .addComponent(jLabel3)
                    .addComponent(albumLabel))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
        if(autoDeleteOnClose){
            File file = new File(currentImage.getPath());
            file.delete();
            log.addInfoLog("Temporary file deleted");
        }
    }//GEN-LAST:event_formWindowClosed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel albumLabel;
    private javax.swing.JScrollPane imageScrollPane;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel nameLabel;
    private javax.swing.JLabel sizeLabel;
    // End of variables declaration//GEN-END:variables

}
